 #include <stdlib.h>    // For malloc() etc.
#include <stdio.h>     // For printf(), fopen() etc.
#include <math.h>      // For sin(), cos() etc.
#include <GL/glfw.h>   // For GLFW, OpenGL and GLU
#include <string.h>

#include "game/physics/world.h"
#include "game/gameplay/gameplay.h"
#include "gfx/gfx_engine.h"
#include "control/mouse.h"
#include "control/keyboard.h"
#include "client/state.h"
#include "client/console.h"

#include "game/gameplay/game_manager.h"

#define ANTANI 0 	/* TODO: ANTANI */

GLfloat rect[8] = {0.0f,0.0f,32.0f,0.0f,32.0f,1.0f,0.0f,1.0f};
GLfloat rect2[8] = {8.0f,8.0f,24.0f,8.0f,24.0f,9.0f,8.0f,9.0f};
GLfloat rect3[8] = {0.0f,1.0f,1.0f,1.0f,1.0f,16.0f,0.0f,16.0f};
GLfloat rect4[8] = {31.0f,1.0f,32.0f,1.0f,32.0f,23.0f,31.0f,23.0f};

//GLfloat rect[8] = {-16.0f,-11.0f,16.0f,-11.0f,16.0f,-12.0f,-16.0f,-12.0f};
//GLfloat rect2[8] = {-16.0f,11.0f,-15.0f,11.0f,-15.0,-11.0,-16.0f,-11.0f};
//GLfloat rect3[8] = {-16.0f,12.0f,16.0f,12.0f,16.0f,11.0f,-16.0f,11.0f};
//GLfloat rect4[8] = {15.0f,11.0f,16.0f,11.0f,16.0,-11.0,15.0f,-11.0f};
GLfloat col1[3] = {1.0f,0.0f,0.0f};
GLfloat col2[3] = {0.0f,1.0f,0.0f};
GLfloat col3[3] = {0.0f,0.0f,1.0f};
GLfloat col4[3] = {0.0f,1.0f,1.0f};

GameState *gameState;
GamePlay *gamePlay;

World *world;
GFXEngine *gfxEngine;

MouseHandler *mouseHandler;
KeyboardHandler *keyboardHandler;
Editor *editor;

WorldPlayer* player;

Variables *variables;

Console *console;

double timeLapsed;

Player *aPlayer;

int main(int argc, char **argv)
{
	aPlayer = new Player();
	
	if(ANTANI)
	{
		
		
	}
	else
	{
		int running; 

			world = new World();
		    
		    variables = new Variables();
		    console = new Console();
		    
		    gamePlay = new GamePlay();
		    gfxEngine = new GFXEngine();
		    gameState = new GameState();
		    
		    player = new WorldPlayer();
		    
		    editor = new Editor();
		    
		    
		    WorldPlayer* player = new WorldPlayer();
		    player->setSize(1.4f,1.4f);
		    WorldArea *area = new WorldArea(8,rect,col1);
		    WorldArea *area2 = new WorldArea(8,rect2,col2);
		    WorldArea *area3 = new WorldArea(8, rect3, col3);
		 /*   WorldArea *area4 = new WorldArea(8, rect4, col4);*/
		   
		    world->addEntity(area);
		    world->addEntity(area2);
		    world->addEntity(area3);
		 /*   world->addEntity(area4); */
		    world->addEntity(player);
		    
		    aPlayer->setWorldEntity((WorldPlayer*)world->getEntity(3));
		    
		    
		    
		    console->appendLine("GeekBattle build 169");

		    
		    /* inizializza la libreria */
			glfwInit();
			console->appendLine("Init glfw...");
		    
		    /* inizializza il nostro potentissimo motore grafico :v */
		    gfxEngine->initEngine();
		    console->appendLine("Init GFX...");

		    mouseHandler = new MouseHandler();
		    console->appendLine("Init mouse...");
		    keyboardHandler = new KeyboardHandler();
		    console->appendLine("Init keyboard...");
		    gameState->setState(STATE_GAME);
		    mouseHandler->stateChanged();
		    keyboardHandler->init();

		    timeLapsed = glfwGetTime();
		    double oldTime = timeLapsed;
		    
		    do
		    {
#ifdef DEBUG		    	
		    	printf("x: %f, y: %f, vx: %f, vy: %f\r\n",player->x,player->y,player->velocity.x,player->velocity.y);
		    	fflush(stdout);
#endif		    	
		    	double delta = timeLapsed - oldTime;

		    	/* chiama la funzione di disegno e disegna il mondo */
		    	gfxEngine->draw(world, delta);
		    	world->updateWorld(delta);

		    	/* TODO: spostare */
		        running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
		        
		        /* TODO: spostare */
		        /*if (glfwGetKey(GLFW_KEY_SPACE))
		        {
		        	gamePlay->playerJumped(player);
		        	//player->velocity.setY(0.1f);
		        }*/
		        
		        if (glfwGetKey('a'))
		        {
		        	gamePlay->playerIsMoving(aPlayer, MOVE_LEFT);
		        }
		        else if (glfwGetKey(GLFW_KEY_RIGHT) == GLFW_PRESS)
		        {
		        	//player->velocity.setX(0.2f);
		        }
		        
		        	//player->velocity.setX(0.0f);
		        
		        oldTime = timeLapsed;
		        timeLapsed = glfwGetTime();
		    }
		    while(running);

		    /* termina l'engine grafico */
		    gfxEngine->termEngine();

	}
    return 0;
}
